package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/17 20:06
 * description:
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;


    @Override
    public DtsAdmin findByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DtsAdmin::getUsername,username);
        DtsAdmin admin=adminMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public IPage<DtsAdmin> queryPage(Integer page, Integer limit, String sort, String order, String username) {

        //创建分页条件参数
        IPage<DtsAdmin> iPage=new Page<>(page,limit);

        QueryWrapper<DtsAdmin> qw=new QueryWrapper<>();
        //分页查询条件
        //如果username不为空，则添加条件
        if(!StringUtils.isEmpty(username)){
            qw.like("username",username);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        adminMapper.selectPage(iPage,qw);


        return iPage;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        //对密码加密
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);
        adminMapper.insert(admin);


        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        String password=admin.getPassword();
        //从数据库中获取密码
        DtsAdmin adminDB =adminMapper.selectById(admin.getId());
        String passwordDB = adminDB.getPassword();
        if(!password.equals(passwordDB)){
            BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
            //对密码加密
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }
        admin.setUpdateTime(new Date());


        adminMapper.updateById(admin);
        return admin;
    }

    @Override
    public void delete(DtsAdmin admin) {
        Integer[] roles=admin.getRoleIds();
        if(Arrays.asList(roles).contains(1)){
            throw new RuntimeException("超级管理员不能删除");
        }
        try {
            adminMapper.deleteById(admin);
        } catch (Exception e) {
            throw new RuntimeException("删除失败");
        }

    }

    @Override
    public List<Map<String, Object>> findAdmin() {
        LambdaQueryWrapper<DtsAdmin> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsAdmin::getDeleted,false);

        List<DtsAdmin> adminList = adminMapper.selectList(qw);
        List<Map<String, Object>> maps = adminList.stream().map(admin -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", admin.getId());
            map.put("label", admin.getUsername());
            return map;
        }).collect(Collectors.toList());
        return maps;
    }
}
